from selenium.webdriver import ActionChains
from hyrobot.common import STEP, CHECK_POINT, INFO
from create_table_super_num import *
import pyautogui
import pytest
import time
import json
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from pymysql_connect import *
from lib.webui_local_mysql import *

# 改了史浩，王鑫尧，刘雨泽为产品管理员 张31，86
class c73250pb:
    # name = '超级管理员录入显示产品数量相关信息'  # 测试用例名字
    cases = [
        ('c73250pb', 'sunxidong', '123456'),

    ]
    def setup(self):
        create_table_for_superior_num()
        STEP(1, '登陆网站')
        wd = open_browser()
        super_login(wd)
    # 清除方法
    def teardown(self):
        wd = get_global_webdriver()
        wd.quit()
    # 测试用例步骤
    def teststeps(self, para_index):

        # 取出参数
        # username, password = self.cases[para_index][1:3]
        # 下面写登录流程
        driver = get_global_webdriver()
        # driver.find_element(By.NAME, "username").send_keys(username)
        # driver.find_element(By.NAME, "password").send_keys(password)
        # driver.find_element(By.CSS_SELECTOR, ".el-button").click()
        time.sleep(1)
        extending_button = driver.find_element_by_id("hamburger-container")
        try:
            driver.find_element(By.CLASS_NAME, "el-tooltip")
            extending_button.click()
        except:
            INFO("左侧栏没有隐藏")
        # click product manage
        time.sleep(1)
        product_manage_word2 = driver.find_element(By.XPATH,
                                                   "/html/body/div/div/div[1]/ul/div[3]/a/li/span"
                                                   )
        product_manage_word2.click()
        time.sleep(1)
        top_title2 = driver.find_element(By.XPATH,
                                         "/html/body/div/div/div[2]/div/div/div[2]/span/span[2]/span[1]/span"
                                         ).text
        CHECK_POINT("上方第3个标题对应", "产品管理" == top_title2)

        STEP(2, "读取表格中产品名称，创建人，显示信息到数据库")
        global_insert_list = []
        first_page_list = get_current_page_insert_list(driver)
        global_insert_list.extend(first_page_list)

        prev_button = driver.find_element_by_class_name("btn-next")
        if not prev_button.get_attribute("disabled"):
            INFO(prev_button.get_attribute("disabled"))
            while prev_button.get_attribute("disabled") == None:
                prev_button.click()
                time.sleep(2)
                current_insert_list = get_current_page_insert_list(driver)
                global_insert_list.extend(current_insert_list)
                time.sleep(1)
                prev_button = driver.find_element_by_class_name("btn-next")
                if prev_button.get_attribute("disabled") == "disabled":
                    break
        INFO(global_insert_list)
        db = connect()
        # 使用cursor()方法获取操作游标
        INFO(3)
        cursor = db.cursor()

        # SQL 插入语句
        sql = "INSERT INTO product_num_for_superior(product_name,version,creater,create_time,update_time,is_display) VALUES (%s,%s,%s,%s,%s,%s)"
        try:
            # 执行sql语句
            cursor.executemany(sql,global_insert_list)
            # 提交到数据库执行
            INFO(4)
            db.commit()
            INFO(5)
            cursor.close()
        except:
            # 如果发生错误则回滚
            db.rollback()
            INFO(6)

        # 关闭数据库连接
        db.close()
        # driver.quit()
